#include "window3.h"
#include<QPainter>
#include"mybutton.h"
#include<QTimer>
#include<QPoint>
#include "mywindow.h"
#include "towerbase1.h"
#include "waypoint.h"
#include "enemy1.h"
#include "bullet1.h"
#include "audioplayer.h"
#include <QMouseEvent>
#include <QtGlobal>
#include <QMessageBox>
#include <QXmlStreamReader>
#include <QtDebug>

window3::window3(QWidget *parent) : QMainWindow(parent), wave(0)

{
    this->setFixedSize(1080,900);
    loadPos();
    add_way();
    MyButton *gold=new MyButton(":/gold.png");
    gold->setParent(this);

   gold->move(38,45);
    gold->setIconSize(QSize(40,40));
    MyButton*db=new MyButton(":/db1.png");
    db->setParent(this);
    db->move(110,94);
    db->setIconSize(QSize(100000,100000));
    MyButton *back=new MyButton(":/back.png");
    back->setParent(this);

    back->move(5,5);
    back->setIconSize(QSize(40,40));
    connect(back,&MyButton::clicked,this,[=](){
       emit Back();




    });
    m_audioPlayer = new AudioPlayer(this);
    m_audioPlayer->startBGM();
    MyButton * startwave = new MyButton(":/sta.png");
    startwave->setParent(this);
    startwave->move(500,750);
    connect(startwave, SIGNAL(clicked()),this,SLOT(update_wave()));
    QTimer *_timer=new QTimer(this);
    connect(_timer,SIGNAL(timeout()),this,SLOT(update_map()));
    _timer->start(30);

}


void window3::paintEvent(QPaintEvent *){
    QPixmap pix(":/map3.png");
    if (gameEnd || if_game_win)
    {
        QString text = gameEnd ? "YOU LOST!!!" : "YOU WIN!!!";
        QPainter painter(this);
        painter.setPen(QPen(Qt::red));
        painter.drawText(rect(), Qt::AlignCenter, text);
        return;
    }
    QPainter cachePainter(&pix);
    show_info(&cachePainter);
    foreach (const TowerPosition &base, towerPositionsList)
        base.draw(&cachePainter);

    foreach (towerbase1 *tower,towersList)
        tower->draw(&cachePainter);

    foreach (waypoint *_wayPoint, waypoint_list)
        _wayPoint->draw(&cachePainter);

    foreach (Enemy1 *_enemy, m_enemyList)
        _enemy->draw(&cachePainter);

    foreach (bullet1 *_bulluet, m_bulletList)
        _bulluet->draw(&cachePainter);
    QPainter painter(this);
    painter.drawPixmap(0, 0, pix);
}


void window3::updatescreen(){
    update();
}



void window3::loadPos()
{
    QPoint pos[] =
    {

        QPoint(570,500),
         QPoint(470,500),
         QPoint(370,500),
        QPoint(800,400),
        QPoint(800,500),
        QPoint(800,220)

    };
    int len	= sizeof(pos) / sizeof(pos[0]);

    for (int i = 0; i < len; ++i)
        towerPositionsList.push_back(pos[i]);
}

bool window3::ifbuy() const
{
    return true;
}
void window3::mousePressEvent(QMouseEvent *event)
{
    QPoint pressPos = event->pos();
    auto it = towerPositionsList.begin();
    while (it != towerPositionsList.end())
    {
        if (ifbuy() && it->containPoint(pressPos) && !it->hasTower())
        {
            it->setHasTower();
            QPoint centerpoint=it->centerPos();
            towerbase1 *tower1 = new towerbase1(centerpoint,this,QPixmap(":/xwk.gif"));
            towersList.push_back(tower1);
            update();
            break;
        }
        ++it;
    }
}




void window3::add_way(){
    waypoint *point1=new waypoint(QPoint(350,605));
    waypoint_list.push_back(point1);

    waypoint *point2=new waypoint(QPoint(875,605));
    waypoint_list.push_back(point2);
    point2->set_next_point(point1);
    waypoint *point3=new waypoint(QPoint(875,200));
    waypoint_list.push_back(point3);
    point3->set_next_point(point2);



}


void window3::Hp_damage(int damage){
base_health-=damage;
if(base_health<=0)
    do_gameover();
}

void window3::remove_enemy(Enemy1 *_enemy){
    Q_ASSERT(_enemy);
    m_enemyList.removeOne(_enemy);
    delete _enemy;
    if(m_enemyList.empty()){
        ++wave;
        if(!loadwave()){
            if_game_win=true;
        }
    }
}
bool window3::loadwave(){
    if(wave>=6)
        return false;
    waypoint *start=waypoint_list.back();
    int enemy_start_interval[]={100,500,600,1000,3000,6000};
    for(int i=0;i<6;++i){
        Enemy1 *_enemy=new Enemy1(start,this,QPixmap(":/m6.gif"));
        m_enemyList.push_back(_enemy);
        QTimer::singleShot(enemy_start_interval[i],_enemy,SLOT(doActivate()));

    }
    return true;
}
void window3::update_map(){
   foreach(Enemy1 *_enemy,m_enemyList)
        _enemy->move();
    foreach(towerbase1 *_tower,towersList)
        _tower->check_enemy_range();
   update();
}

void window3::add_bulluet(bullet1 *_bulluet)
{
    Q_ASSERT(_bulluet);

    m_bulletList.push_back(_bulluet);
}
void window3::remove_bulluet(bullet1 *bulluet)
{
    Q_ASSERT(bulluet);
    m_bulletList.removeOne(bulluet);
    delete bulluet;
}
void window3::awardgold(int gold)
{
    fee+=gold;
}
void window3::show_info(QPainter *painter){
    painter->save();
    painter->setPen(Qt::red);
    painter->drawText(QRect(10,50,2000,50),QString("gold     :%1").arg(fee));
    painter->drawText(QRect(10,100,2000,50),QString("the remian balls     :%2").arg(base_health));
    painter->restore();
}
void window3::do_gameover(){
    gameEnd=true;
}
QList<Enemy1*> window3::enemy_list() const{
    return m_enemyList;

}
void window3::update_wave(){
    loadwave();
}


